package net.lainiao.zhihu.backserver;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2016/1/8.
 */
@Service
public class BackServer implements ApplicationContextAware {
    private static ApplicationContext context;
    private static String server;
    private static ZhihuhuatiTask zhihuhuatiTask;
    private static ZhihuhuatiPageTask zhihuhuatiPageTask;
    private static ZhihuhuatiPageLinkTask zhihuhuatiPageLinkTask;
    private static QutuTask qutuTask;
    private static QutuPageTask qutuPageTask;
    public BackServer() {

    }

    public static String setServer(String mess) {
        if (mess.equals("A1")) {
            if (zhihuhuatiTask == null) {
                zhihuhuatiTask = new ZhihuhuatiTask(context);
            }
            Thread thread = new Thread(zhihuhuatiTask);
            zhihuhuatiTask.status = 1;
            thread.start();
            return "启动知乎话题解析服务成功";
        }
        if (mess.equals("A0")) {
            zhihuhuatiTask.status = 0;
            return "停止知乎话题解析服务";
        }
        if (mess.equals("B1")) {
            if (zhihuhuatiPageTask == null) {
                zhihuhuatiPageTask = new ZhihuhuatiPageTask(context);
            }
            Thread thread = new Thread(zhihuhuatiPageTask);
            zhihuhuatiPageTask.status = 1;
            thread.start();
            return "启动知乎话题Page服务成功";
        }
        if (mess.equals("B0")) {
            zhihuhuatiPageTask.status = 0;
            return "停止知乎话题Page服务成功";
        }
        if (mess.equals("C1")) {
            if (zhihuhuatiPageLinkTask == null) {
                zhihuhuatiPageLinkTask = new ZhihuhuatiPageLinkTask(context);
            }
            Thread thread = new Thread(zhihuhuatiPageLinkTask);
            zhihuhuatiPageLinkTask.status = 1;
            thread.start();
            return "启动知乎话题Link服务成功";
        }
        if (mess.equals("C0")) {
            zhihuhuatiPageLinkTask.status = 0;
            return "停止知乎话题Link服务成功";
        }

        if (mess.equals("D1")) {
            if (qutuTask == null) {
                qutuTask = new QutuTask(context);
            }
            Thread thread = new Thread(qutuTask);
            qutuTask.status = 1;
            thread.start();
            return "启动趣图服务成功";
        }
        if (mess.equals("D0")) {
            qutuTask.status = 0;
            return "停止趣图服务成功";
        }

        if (mess.equals("E1")) {
            if (qutuPageTask == null) {
                qutuPageTask = new QutuPageTask(context);
            }
            Thread thread = new Thread(qutuPageTask);
            qutuPageTask.status = 1;
            thread.start();
            return "启动趣图Page服务成功";
        }
        if (mess.equals("E0")) {
            qutuPageTask.status = 0;
            return "停止趣图Page服务成功";
        }
        return "未知的服务";
    }

    /**
     * 启动服务
     */
    public synchronized void start() throws Exception {

    }

    /**
     * 停止服务
     */
    public synchronized void stop() {

    }

    /**
     * 设置全局容器
     *
     * @param applicationContext
     * @throws BeansException
     */
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}

